//
//  MovieThreeViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MovieThreeViewController.h"
#import "MovieModel.h"
#import "MovieThreeCell.h"
#import "UIImageView+WebCache.h"

@interface MovieThreeViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *bigArr;
@property (nonatomic, retain) NSMutableArray *littleArr;

@end

@implementation MovieThreeViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(verticalMovie:)];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.tableView registerClass:[MovieThreeCell class] forCellReuseIdentifier:@"cell"];
    
  
    [self dataHandle];
    
    
#pragma mark - 划分大小数组
    
    self.bigArr = [NSMutableArray array];
    NSInteger temp = 0;
    for (MovieModel *model in _arrModel) {
        if (temp == 0) {
            self.littleArr = [NSMutableArray array];
        }
        
       [_littleArr addObject:model];
        temp++;
        
        if (temp == 3) {
            [_bigArr addObject:_littleArr];
            temp = 0;
        }
    
    }
    [_bigArr addObject:_littleArr];
    NSLog(@"%ld", _bigArr.count);
}


- (void)verticalMovie:(UIBarButtonItem *)button {
//    MovieViewController *movie = [[MovieViewController alloc] init];

    [self.navigationController popToRootViewControllerAnimated:NO];
//    [self.navigationController pushViewController:movie animated:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _bigArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    

    NSMutableArray *littleArr = _bigArr[indexPath.row];
    
    if (indexPath.row == _bigArr.count - 1) {
        
        MovieModel *movieModel0 = littleArr[0];
        MovieModel *movieModel1 = littleArr[1];
        
        [cell.moviePic1 sd_setImageWithURL:[NSURL URLWithString:movieModel0.image]];
        cell.movieName1.text = movieModel0.title;
        
        [cell.moviePic2 sd_setImageWithURL:[NSURL URLWithString:movieModel1.image]];
        cell.movieName2.text = movieModel1.title;
        
        cell.moviePic3.image = nil;
        cell.movieName3.text = nil;
        
       
    }else {
        
        MovieModel *movieModel0 = littleArr[0];
        MovieModel *movieModel1 = littleArr[1];
        MovieModel *movieModel2 = littleArr[2];
        
        [cell.moviePic1 sd_setImageWithURL:[NSURL URLWithString:movieModel0.image]];
        cell.movieName1.text = movieModel0.title;
        
        [cell.moviePic2 sd_setImageWithURL:[NSURL URLWithString:movieModel1.image]];
        cell.movieName2.text = movieModel1.title;
        
        [cell.moviePic3 sd_setImageWithURL:[NSURL URLWithString:movieModel2.image]];
        cell.movieName3.text = movieModel2.title;
    }
    
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}

- (void)dataHandle {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        MovieModel *movieModel = [MovieModel movie];
        [movieModel setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:movieModel];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
